package com.cy.pj.modules.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.common.web.domain.BaseController;
import com.cy.pj.modules.entity.SpuSpecification;
import com.cy.pj.modules.service.ISpuSpecificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 商品规格Controller
 *
 * @author kiven
 * @date 2023-02-09
 */
@Controller
@RequestMapping("/specification")
public class SpuSpecificationController extends BaseController
{
    private String prefix = "modules/specification";

    @Autowired
    private ISpuSpecificationService spuSpecificationService;

    @RequiresPermissions("specification:view")
    @GetMapping()
    public String specification()
    {
        return prefix + "/specification";
    }


    @GetMapping("/bySpuId/{spuId}")
    @ResponseBody
    public JsonResult bySpuId(@PathVariable("spuId") String spuId){
        return new JsonResult(spuSpecificationService.selectSpuSpecificationListBySpuId(spuId));
    }

    /**
     * 查询商品规格列表
     */
    @RequiresPermissions("specification:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(SpuSpecification spuSpecification,Integer pageCurrent)
    {
        return new JsonResult(spuSpecificationService.selectSpuSpecificationList(spuSpecification,pageCurrent));
    }

    ///**
    // * 导出商品规格列表
    // */
    //@RequiresPermissions("specification:export")
    //@PostMapping("/export")
    //@ResponseBody
    //public AjaxResult export(SpuSpecification spuSpecification)
    //{
    //    List<SpuSpecification> list = spuSpecificationService.selectSpuSpecificationList(spuSpecification);
    //    ExcelUtil<SpuSpecification> util = new ExcelUtil<SpuSpecification>(SpuSpecification.class);
    //    return util.exportExcel(list, "specification");
    //}

    /**
     * 新增商品规格
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品规格
     */
    @RequiresPermissions("specification:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(SpuSpecification spuSpecification)
    {
        return new JsonResult(spuSpecificationService.insertSpuSpecification(spuSpecification));
    }

    /**
     * 修改商品规格
     */
    @GetMapping("/edit/")
    public String edit( Long id, ModelMap mmap)
    {
        SpuSpecification spuSpecification = spuSpecificationService.selectSpuSpecificationById(id);
        mmap.put("spuSpecification", spuSpecification);
        return prefix + "/specification_edit";
    }

    @GetMapping("/id")
    @ResponseBody
    public JsonResult byId(Long id){
        return new JsonResult(spuSpecificationService.selectSpuSpecificationById(id));
    }
    /**
     * 修改保存商品规格
     */
    @RequiresPermissions("specification:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(SpuSpecification spuSpecification)
    {
        return new JsonResult(spuSpecificationService.updateSpuSpecification(spuSpecification));
    }

    /**
     * 删除商品规格
     */
    @RequiresPermissions("specification:delete")
    @PostMapping( "/remove")
    @ResponseBody
    public JsonResult remove(String ids)
    {

        return new JsonResult(spuSpecificationService.deleteSpuSpecificationByIds(ids));
    }

}
